* ctype_* compatability functions
[lhc/web/wiklou.git] / includes / compatability / ctype.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die();
4 /**
5 * PHP <4.2.0 doesn't build ctype_ functions by default and Gentoo doesn't
6 * build it by default, and that probably applies for some other distributions
7 *
8 * These functions should be fully compatable with their PHP equivalents
9 *
10 * @package MediaWiki
11 * @subpackage Compatability
12 *
13 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
14 * @copyright Copyright © 2006, Ævar Arnfjörð Bjarmason
15 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
16 *
17 * @link http://perldoc.perl.org/perlre.html perldoc perlre
18 *
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version 2
22 * of the License, or (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32 */
33
34 /**#@+
35 * Takes the same input and returns the same values as the equvalent PHP
36 * function
37 *
38 * @param mixed $in
39 * @return bool
40 */
41 function ctype_alnum() {
42 $fname = 'ctype_alnum';
43
44 $args = func_get_args();
45 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
46
47 if ( ! is_null( $ret ) )
48 return $ret;
49 else
50 return (bool)preg_match( '~^[[:alnum:]]+$~i', $in );
51 }
52
53 function ctype_alpha() {
54 $fname = 'ctype_alpha';
55
56 $args = func_get_args();
57 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
58
59 if ( ! is_null( $ret ) )
60 return $ret;
61 else
62 return (bool)preg_match( '~^[[:alpha:]]+$~i', $in );
63 }
64
65 function ctype_cntrl() {
66 $fname = 'ctype_cntrl';
67
68 $args = func_get_args();
69 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
70
71 if ( ! is_null( $ret ) )
72 return $ret;
73 else
74 return (bool)preg_match( '~^[[:cntrl:]]+$~', $in );
75 }
76
77 function ctype_digit() {
78 $fname = 'ctype_digit';
79
80 $args = func_get_args();
81 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
82
83 if ( ! is_null( $ret ) )
84 return $ret;
85 else
86 return (bool)preg_match( '~^[[:digit:]]+$~', $in );
87 }
88
89 function ctype_graph() {
90 $fname = 'ctype_graph';
91
92 $args = func_get_args();
93 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
94
95 if ( ! is_null( $ret ) )
96 return $ret;
97 else
98 return (bool)preg_match( '~^[[:graph:]]+$~', $in );
99 }
100
101 function ctype_lower() {
102 $fname = 'ctype_lower';
103
104 $args = func_get_args();
105 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
106
107 if ( ! is_null( $ret ) )
108 return $ret;
109 else
110 return (bool)preg_match( '~^[[:lower:]]+$~', $in );
111 }
112
113 function ctype_print() {
114 $fname = 'ctype_print';
115
116 $args = func_get_args();
117 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
118
119 if ( ! is_null( $ret ) )
120 return $ret;
121 else
122 return (bool)preg_match( '~^[[:print:]]+$~', $in );
123 }
124
125 function ctype_punct() {
126 $fname = 'ctype_punct';
127
128 $args = func_get_args();
129 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
130
131 if ( ! is_null( $ret ) )
132 return $ret;
133 else
134 return (bool)preg_match( '~^[[:punct:]]+$~', $in );
135 }
136
137 function ctype_space() {
138 $fname = 'ctype_space';
139
140 $args = func_get_args();
141 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
142
143 if ( ! is_null( $ret ) )
144 return $ret;
145 else
146 return (bool)preg_match( '~^[[:space:]]+$~', $in );
147
148 }
149
150
151 function ctype_upper() {
152 $fname = 'ctype_upper';
153
154 $args = func_get_args();
155 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
156
157 if ( ! is_null( $ret ) )
158 return $ret;
159 else
160 return (bool)preg_match( '~^[[:upper:]]+$~', $in );
161
162 }
163
164 function ctype_xdigit() {
165 $fname = 'ctype_xdigit';
166
167 $args = func_get_args();
168 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
169
170 if ( ! is_null( $ret ) )
171 return $ret;
172 else
173 return (bool)preg_match( '~^[[:xdigit:]]+$~i', $in );
174 }
175
176
177
178
179 function _ctype_parse_args( $fname, $args ) {
180 $ret = null;
181
182 $cnt = count( $args );
183
184 if ( $cnt !== 1 )
185 // PHP only throws E_ERROR on this, but fuck it;)
186 wfDebugDieBacktrace( "Error: $fname() expects exactly 1 parameter $cnt given" );
187
188 $in = array_pop( $args );
189
190 if ( is_int( $in ) ) {
191 if ( $in >= 256 )
192 // >= 256 returns false, except in the case of these functions
193 return array(
194 null,
195 $fname === 'ctype_alnum' ||
196 $fname === 'ctype_digit' ||
197 $fname === 'ctype_graph' ||
198 $fname === 'ctype_print' ||
199 $fname === 'ctype_xdigit'
200 );
201 else if ( $in >= 0 )
202 $in = chr( $in );
203 else if ( $in >= -128 )
204 $in = ord( $in + 256 );
205 else if ( $in < -128 )
206 // <-128 values return false, except in the case of these functions
207 return
208 array(
209 null,
210 $fname === 'ctype_graph' || $fname === 'ctype_print'
211 );
212 }
213
214 if ( is_string( $in ) )
215 if ( $in === '' )
216 return array( null, true );
217 else
218 return array( $in, null );
219 else
220 // Like PHP
221 return array( null, false );
222 }